﻿using System;

using System.Data.SqlClient;
using System.Text;

namespace IFD2
{
    public partial class Surcharges : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            btnAdd.Attributes.Add("onclick", "javascript:return confirm('Are you sure?');");
            btnDelete.Attributes.Add("onclick", "javascript:return confirm('Are you sure?');");
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (Session["ACL"].ToString().IndexOf("FB5") < 0)
            {
                lblError.Text = "Unauthorized!";
                return;
            }
            
            string key;
            string[] strArray;
            if (Request.Form["KEY"] == null)
            {
                lblError.Text = "Please check the record which you want to submit!";
                return;
            }
            key = Request.Form["KEY"];
            strArray = key.Split('|');

            int ret_code;
            SqlCommand cmd = new SqlCommand();
            lblError.Text = "";
            txtCode.Text = txtCode.Text.Trim().ToUpper();
            try
            {
                cmd.CommandText = "bll_surcharge_delete";
                SqlParameter para = cmd.Parameters.AddWithValue("@airline", Session["AIRLINE"].ToString());
                para = cmd.Parameters.AddWithValue("@code", strArray[0]);
                para = cmd.Parameters.AddWithValue("@flt_surcharge_types", strArray[2]);
                para = cmd.Parameters.AddWithValue("@effective_time", strArray[1]);
                para = cmd.Parameters.AddWithValue("@user", Session["USER"].ToString());
                para = cmd.Parameters.AddWithValue("@office", Session["OFFICE"].ToString());
                para = cmd.Parameters.AddWithValue("@ip_addr", Request.UserHostAddress.ToString());
                ret_code = TinyDAL.Exec(ref cmd);
                if (ret_code == 0)
                {
                    lblError.Text = "Done";
                    GridView1.DataBind();
                }
                else if (ret_code == 1)
                    lblError.Text = "deleting failed!";
                else
                    lblError.Text = "Error: " + ret_code.ToString();
            }
            catch (Exception ex)
            {
                if (Application["DEBUG"].ToString() == "1")
                    lblError.Text = ex.Message;
                else
                    lblError.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
            }
        }

        protected void lbtnQuery_Click(object sender, EventArgs e)
        {
            GridView1.DataBind();
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            if (Session["ACL"].ToString().IndexOf("FB5") < 0)
            {
                lblError.Text = "Unauthorized!";
                return;
            }
            lblError.Text = "";
            txtCode.Text = txtCode.Text.Trim().ToUpper();
            txtNote.Text = txtNote.Text.Trim();
            int ret_code;
            try
            {
                SqlCommand cmd = new SqlCommand("bll_surcharge_new");
                SqlParameter para = cmd.Parameters.AddWithValue("@airline", Session["AIRLINE"].ToString());
                para = cmd.Parameters.AddWithValue("@code", txtCode.Text);
                para = cmd.Parameters.AddWithValue("@flt_surcharge_types", txtFltChargeTypes.Text);
                para = cmd.Parameters.AddWithValue("@effective_time",txtEffectiveTime.Text);
                para = cmd.Parameters.AddWithValue("@type", lstTypes.SelectedValue);
                para = cmd.Parameters.AddWithValue("@currency", lstCurrencies.SelectedValue);
                para = cmd.Parameters.AddWithValue("@adt_price", System.Convert.ToDecimal(txtAdtPrice.Text));
                para = cmd.Parameters.AddWithValue("@chd_price", System.Convert.ToDecimal(txtChdPrice.Text));
                para = cmd.Parameters.AddWithValue("@inf_price", System.Convert.ToDecimal(txtInfPrice.Text));
                para = cmd.Parameters.AddWithValue("@note", txtNote.Text);
                para = cmd.Parameters.AddWithValue("@user", Session["USER"].ToString());
                para = cmd.Parameters.AddWithValue("@office", Session["OFFICE"].ToString());
                para = cmd.Parameters.AddWithValue("@ip_addr", Request.UserHostAddress.ToString());
                ret_code = TinyDAL.Exec(ref cmd);
                if (ret_code == 0)
                {
                    lblError.Text = "Done";
                    GridView1.DataBind();
                }
                else if (ret_code == 1)
                    lblError.Text = "Adding failed!";
                else
                    lblError.Text = "Error: " + ret_code.ToString();
            }
            catch (Exception ex)
            {
                if (Application["DEBUG"].ToString() == "1")
                    lblError.Text = ex.Message;
                else
                    lblError.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
            }
        }

        //protected void lstAirlines_DataBound(object sender, EventArgs e)
        //{
        //    lstAirlines.SelectedValue = Session["AIRLINE"].ToString();
        //}

        protected void btnNames_Click(object sender, EventArgs e)
        {
            string key;
            string[] strArray;
            if (Request.Form["KEY"] == null)
            {
                lblError.Text = "Please check the record which you want to submit!";
                return;
            }
            key = Request.Form["KEY"];
            strArray = key.Split('|');

            Response.Redirect("SurchargeNames.aspx?surcharge_code=" + strArray[0]);
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (Session["ACL"].ToString().IndexOf("FB2") < 0)
            {
                lblError.Text = "Unauthorized!";
                return;
            }

            lblError.Text = "";
            int ret_code;

            string key;
            string[] strArray;
            if (Request.Form["KEY"] == null)
            {
                lblError.Text = "Please check the record which you want to submit!";
                return;
            }
            key = Request.Form["KEY"];
            strArray = key.Split('|');

            try
            {
                SqlCommand cmd = new SqlCommand("bll_surcharge_submit");
                SqlParameter para = cmd.Parameters.AddWithValue("@airline", Session["AIRLINE"].ToString());
                para = cmd.Parameters.AddWithValue("@code", strArray[0]);
                para = cmd.Parameters.AddWithValue("@flt_surcharge_types", strArray[2]);
                para = cmd.Parameters.AddWithValue("@effective_time", strArray[1]);
                para = cmd.Parameters.AddWithValue("@user", Session["USER"].ToString());
                para = cmd.Parameters.AddWithValue("@office", Session["OFFICE"].ToString());
                para = cmd.Parameters.AddWithValue("@ip_addr", Request.UserHostAddress.ToString());
                ret_code = TinyDAL.Exec(ref cmd);
                if (ret_code == 0)
                {
                    lblError.Text = "Done";
                    GridView1.DataBind();
                }
                else if (ret_code == 1)
                    lblError.Text = "Submition failed!";
                else
                    lblError.Text = "Error: " + ret_code.ToString();
            }
            catch (Exception ex)
            {
                if (Application["DEBUG"].ToString() == "1")
                    lblError.Text = ex.Message;
                else
                    lblError.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
            }
        }

        protected void btnApprove_Click(object sender, EventArgs e)
        {
            if (Session["ACL"].ToString().IndexOf("FB4") < 0)
            {
                lblError.Text = "Unauthorized!";
                return;
            }

            lblError.Text = "";
            int ret_code;

            string key;
            string[] strArray;
            if (Request.Form["KEY"] == null)
            {
                lblError.Text = "Please check the record which you want to approve!";
                return;
            }
            key = Request.Form["KEY"];
            strArray = key.Split('|');

            try
            {
                SqlCommand cmd = new SqlCommand("bll_surcharge_approve");
                SqlParameter para = cmd.Parameters.AddWithValue("@airline", Session["AIRLINE"].ToString());
                para = cmd.Parameters.AddWithValue("@code", strArray[0]);
                para = cmd.Parameters.AddWithValue("@flt_surcharge_types", strArray[2]);
                para = cmd.Parameters.AddWithValue("@effective_time", strArray[1]);
                para = cmd.Parameters.AddWithValue("@user", Session["USER"].ToString());
                para = cmd.Parameters.AddWithValue("@office", Session["OFFICE"].ToString());
                para = cmd.Parameters.AddWithValue("@ip_addr", Request.UserHostAddress.ToString());
                ret_code = TinyDAL.Exec(ref cmd);
                if (ret_code == 0)
                {
                    lblError.Text = "Done";
                    GridView1.DataBind();
                }
                else if (ret_code == 1)
                    lblError.Text = "Approvement failed!";
                else
                    lblError.Text = "Error: " + ret_code.ToString();
            }
            catch (Exception ex)
            {
                if (Application["DEBUG"].ToString() == "1")
                    lblError.Text = ex.Message;
                else
                    lblError.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
            }
        }

        protected void btnReject_Click(object sender, EventArgs e)
        {
            if (Session["ACL"].ToString().IndexOf("FB4") < 0)
            {
                lblError.Text = "Unauthorized!";
                return;
            }

            lblError.Text = "";
            int ret_code;

            string key;
            string[] strArray;
            if (Request.Form["KEY"] == null)
            {
                lblError.Text = "Please check the record which you want to reject!";
                return;
            }
            key = Request.Form["KEY"];
            strArray = key.Split('|');

            try
            {
                SqlCommand cmd = new SqlCommand("bll_surcharge_reject");
                SqlParameter para = cmd.Parameters.AddWithValue("@airline", Session["AIRLINE"].ToString());
                para = cmd.Parameters.AddWithValue("@code", strArray[0]);
                para = cmd.Parameters.AddWithValue("@flt_surcharge_types", strArray[2]);
                para = cmd.Parameters.AddWithValue("@effective_time", strArray[1]);
                para = cmd.Parameters.AddWithValue("@user", Session["USER"].ToString());
                para = cmd.Parameters.AddWithValue("@office", Session["OFFICE"].ToString());
                para = cmd.Parameters.AddWithValue("@ip_addr", Request.UserHostAddress.ToString());
                ret_code = TinyDAL.Exec(ref cmd);
                if (ret_code == 0)
                {
                    lblError.Text = "Done";
                    GridView1.DataBind();
                }
                else if (ret_code == 1)
                    lblError.Text = "Rejection failed!";
                else
                    lblError.Text = "Error: " + ret_code.ToString();
            }
            catch (Exception ex)
            {
                if (Application["DEBUG"].ToString() == "1")
                    lblError.Text = ex.Message;
                else
                    lblError.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            if (Session["ACL"].ToString().IndexOf("FB4") < 0)
            {
                lblError.Text = "Unauthorized!";
                return;
            }

            lblError.Text = "";
            int ret_code;

            string key;
            string[] strArray;
            if (Request.Form["KEY"] == null)
            {
                lblError.Text = "Please check the record which you want to cancel!";
                return;
            }
            key = Request.Form["KEY"];
            strArray = key.Split('|');

            try
            {
                SqlCommand cmd = new SqlCommand("bll_surcharge_cancel");
                SqlParameter para = cmd.Parameters.AddWithValue("@airline", Session["AIRLINE"].ToString());
                para = cmd.Parameters.AddWithValue("@code", strArray[0]);
                para = cmd.Parameters.AddWithValue("@flt_surcharge_types", strArray[2]);
                para = cmd.Parameters.AddWithValue("@effective_time", strArray[1]);
                para = cmd.Parameters.AddWithValue("@user", Session["USER"].ToString());
                para = cmd.Parameters.AddWithValue("@office", Session["OFFICE"].ToString());
                para = cmd.Parameters.AddWithValue("@ip_addr", Request.UserHostAddress.ToString());
                ret_code = TinyDAL.Exec(ref cmd);
                if (ret_code == 0)
                {
                    lblError.Text = "Done";
                    GridView1.DataBind();
                }
                else if (ret_code == 1)
                    lblError.Text = "Canceling failed!";
                else
                    lblError.Text = "Error: " + ret_code.ToString();
            }
            catch (Exception ex)
            {
                if (Application["DEBUG"].ToString() == "1")
                    lblError.Text = ex.Message;
                else
                    lblError.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
            }
        }

        protected void btnLog_Click(object sender, EventArgs e)
        {
            string key;
            string[] strArray;
            if (Request.Form["KEY"] == null)
            {
                lblError.Text = "Please check the record which you want to cancel!";
                return;
            }
            key = Request.Form["KEY"];
            strArray = key.Split('|');
            Response.Redirect("SurchargeLog.aspx?code=" + strArray[0]);
        }

        protected void btnSegments_Click(object sender, EventArgs e)
        {
            string key;
            string[] strArray;
            if (Request.Form["KEY"] == null)
            {
                lblError.Text = "Please check the record which you want to cancel!";
                return;
            }
            key = Request.Form["KEY"];
            strArray = key.Split('|');
            Response.Redirect("SurchargeSegs.aspx?code=" + strArray[0] + "&effective_time=" + strArray[1] + "&flt_surcharge_types=" + strArray[2]);
        }
    }
}